package com.jwdf.uda.ui.fragment.question.index;
import android.annotation.SuppressLint;
import android.app.Activity;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.NewProblemsEvent;
import com.jwdf.uda.event.QuizDetailEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.QuizIndexAction;
import com.jwdf.uda.ui.adapter.QuizQuickAdapter;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;
import com.jwdf.uda.utils.EventBusUtil;

import java.util.List;

import retrofit2.Response;

import static com.jwdf.uda.utils.EventBusUtil.unregisterEvent;
/**
 * Created by fatsfan on 2016/9/8.
 */
@SuppressLint("ValidFragment")
public class QuizQuickIndexFragment extends SimpleListFragment {

    protected Activity aty;
    protected PbBean.PbSignBaseQuestionType restType = PbBean.PbSignBaseQuestionType.QUESTION_INDEX_NONE;
    private QuizQuickAdapter mAdapter;

    @Override
    protected void initViews() {
        mAdapter = new QuizQuickAdapter(getActivity(), this);
        lv.setAdapter(mAdapter);
        aty = getActivity();
        EventBusUtil.registerEvent(this);
    }
    @Override
    protected void onListItemClick(int position) {

    }
    @Override
    protected void requestData(final boolean refresh) {

        int offset = refresh ? 0 : mAdapter.getCount();
        onNewRequestCall(QuizIndexAction.newInstance(getContext(), offset, restType).request(new AHttpService.IResCallback<PbBean.PbResQuestionQuestionList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResQuestionQuestionList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(getView() != null) {
                    closeProgressDialog();
                    if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        mDataBinding.commonFragmentList.stopRefresh();
                        mDataBinding.commonFragmentList.stopLoadMore();
                        return;
                    }
                    doSuccessResponse(refresh, response.body());
                }
            }
        }));
    }
    protected void doSuccessResponse(boolean refresh, PbBean.PbResQuestionQuestionList body) {
        if(body != null) {
            final List<PbBean.PbBaseQuestion> questionInfoList = body.getQuestionInfoList();
            if(refresh) {
                mAdapter.setData(questionInfoList);
            } else {
                mAdapter.append(questionInfoList);
            }

            if(questionInfoList.size() == 0)
                mDataBinding.commonFragmentList.setEmptyView(mDataBinding.tvEmptyData);

            mDataBinding.commonFragmentList.setPullLoadEnable(questionInfoList.size() == getPageLength());
            mDataBinding.commonFragmentList.stopRefresh();
            mDataBinding.commonFragmentList.stopLoadMore();
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unregisterEvent(this);
    }
    public void onEventMainThread(NewProblemsEvent event) {
        switch(event.getEvent()) {
            case NewProblemsEvent.EVENT_COMMENT_DELETE:
            case NewProblemsEvent.EVENT_COMMENT_RELEASE:
                requestData(true);
                break;
        }
    }

    public void onEventMainThread(QuizDetailEvent event) {

        if(event.getEvent() == QuizDetailEvent.EVENT_QUIZ_REPLY_ADD) {
            requestData(true);
        }
    }

}
